#include "s_spi.h"
#include <spi/spi.h>

//------------------------------------------------------------------------------
/// Initialize SPI to work with AD7708
//------------------------------------------------------------------------------
void SPI_Initialize()
{
    SPI_Configure(BOARD_SPI_BASE, BOARD_SPI_ID, AT91C_SPI_MSTR | AT91C_SPI_PS_FIXED | (0xE << 16) | AT91C_SPI_MODFDIS);
    
    SPI_ConfigureNPCS(BOARD_SPI_BASE, BOARD_ADC_NPCS, ((0x30 << 8) | /*AT91C_SPI_NCPHA |*/ AT91C_SPI_CSAAT | AT91C_SPI_CPOL | AT91C_SPI_BITS_16 | (AT91C_SPI_DLYBS & (1 << 16)) | (AT91C_SPI_DLYBCT & 0)));
   
    SPI_Enable(AT91C_BASE_SPI);
}

//------------------------------------------------------------------------------
/// Write data to slave device and read from him simultaneously
//------------------------------------------------------------------------------
unsigned short SPI_Read_Write(unsigned short WriteData)
{
    SPI_Write(AT91C_BASE_SPI, BOARD_ADC_NPCS, WriteData);
    return SPI_Read(AT91C_BASE_SPI);
}
